//
//  LocationManager.m
//  应用内跳转至高德地图
//
//  Created by 李佳楠 on 2017/1/23.
//  Copyright © 2017年 李佳楠. All rights reserved.
//

#import "LocationManager.h"
@interface LocationManager()
@property (nonatomic,strong)CLGeocoder * geocoder;
@property (nonatomic,strong)CLLocation * location1;

@end
static LocationManager *instance;
@implementation LocationManager
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @synchronized (self) {
            instance = [[self alloc] init];
        }
    });
    return instance;
}
-(instancetype)init
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @synchronized (self) {
            instance = [super init];
        }
    });
    return instance;
}

#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address regionReturn:(void (^) (CLLocationCoordinate2D coordinate))coordinateBlock{
    //地理编码
    [[[CLGeocoder alloc]init] geocodeAddressString:address completionHandler:^(NSArray *placemarks,NSError *error) {
        CLPlacemark *placemark=[placemarks firstObject];
        CLLocationCoordinate2D coordinate = placemark.location.coordinate;
        coordinateBlock(coordinate);
    }];
}

#pragma mark 根据地理坐标确定地名
-(void)getAddressByCoordinate:(NSString *)latitudeText  with:(NSString *)longtitudeText{
    
    CLLocationDegrees latitude=[latitudeText doubleValue];
    CLLocationDegrees longitude=[longtitudeText doubleValue];
    
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    
    //2.反地理编码
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks,NSError *error) {
        if (error||placemarks.count==0) {
//            self.reverdeDetailAddressLabel.text=@"你输入的地址没找到";
        }else//编码成功
        {
            //显示最前面的地标信息
            CLPlacemark *firstPlacemark=[placemarks firstObject];
//            self.reverdeDetailAddressLabel.text=firstPlacemark.name;
            //经纬度
            CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
            CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
//            self.latitudeField.text=[NSString stringWithFormat:@"%.2f",latitude];
//            self.longitudeField.text=[NSString stringWithFormat:@"%.2f",longitude];
        }
    }];
}
@end
